package com.iflytek.yousheng.Util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class SignUtils2 {
    private static final String SEPARATOR = "\u0026";
    private static final String appSecret = "9332ba31a9cb4e948d6b0928d48c90b7";
    private static String buildSignStr(JSONObject request, String secret) {
        StringBuilder builder = new StringBuilder();
        List<String> keys = new LinkedList<>();
        keys.addAll(request.keySet());
        keys.remove("sign");
        Collections.sort(keys);
        for (String key : keys) {
            Object obj = request.get(key);
            if (obj instanceof JSONObject) {
                if (((JSONObject) obj).size() == 0) {
                    continue;
                }
                builder.append(key).append("=").append(buildSignStr((JSONObject) obj, secret)).append(SEPARATOR);
            } else if (obj instanceof JSONArray) {
                StringBuilder sb = new StringBuilder();
                JSONArray array = (JSONArray) obj;
//                Iterator iterator = array.iterator();
/*                if (iterator.hasNext()){
                    Object json = iterator.next();
                    sb.append( json instanceof String ? json : buildSignStr((JSONObject) json, secret)).append(",");
                }*/
                array.forEach(json -> sb.append(json instanceof String ? json : buildSignStr((JSONObject) json, secret)).append(","));
//                array.forEach(json->sb.append(buildSignStr((JSONObject) json, secret)).append(","));
                if (sb.length()>0){
                    sb.deleteCharAt(sb.length()-1);
                }
                builder.append(key).append("=").append(sb.toString()).append(SEPARATOR);
            } else {
                if (obj instanceof CharSequence && StringUtils.isBlank((CharSequence) obj)) {
                    continue;
                }
                builder.append(key).append("=").append(obj).append(SEPARATOR);
            }
        }
        int lenth = builder.length();
        if (lenth > 0) {
            builder.deleteCharAt(lenth - 1);
        }
        return builder.toString();
    }

    public static String buildSign(JSONObject request, String secret) {
        if (request == null) {
            return null;
        }
        String signStr = buildSignStr(request, secret) + SEPARATOR + secret;
        System.out.println("signStr"+signStr);
        return DigestUtils.md5Hex(signStr);
    }

//    public static String buildSign(JSONObject request) {
//        if (request == null) {
//            return null;
//        }
//        String signStr = buildSignStr(request, appSecret) + SEPARATOR + appSecret;
//        System.out.println(signStr);
//        return DigestUtils.md5Hex(signStr);
//    }

    public static void main(String[] args) {
        JSONObject request = new JSONObject();
        // 创建param JSONObject
        JSONObject param = new JSONObject();
        param.put("scale", 1.0f);
        param.put("text", "大家好，我是小颜，欢迎使用智能做课平台。");
        param.put("height", "1920");
        param.put("vcn", "x3_panting");
        param.put("subtitles", 0);
        param.put("format", "webm");
        param.put("anchorId", 110105012);
        param.put("width", "1080");

        // 创建base JSONObject
        JSONObject base = new JSONObject();
        base.put("appId", "AK61vp61");
        base.put("sign", "54735780343f64e55f3a52e4ad43ff68");
        base.put("timestamp", 1725967274000L);
        base.put("sid", "f813c24b466546cab1df586b04915efd");

        // 将param和base放入request中
        request.put("param", param);
        request.put("base", base);
        System.out.println(buildSign(request, "25631753f51045cea4dcfa8b3a1fb760"));
    }

}
